// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'

Shader "GK/Unlit/PowerGauge" 
{
	Properties 
	{
	    _Color ("Color", Color) = (1,1,1,1)
	    _MainTex ("Main Tex (RGBA)", 2D) = "white" {}
	    _NoEnergyColorMultiplier ("No energy color multiplier", Float) = 0.5
	    _TransitionWidth ("Transition width", Float) = 0.01
	    _UVMin("UV min", Float) = 0.04477612
	    _UVMax("UV max", Float) = 0.4776119
	    _Progress ("Progress", Range(0.0, 1.0)) = 0.0
	}
	
	SubShader 
	{
	    Tags { "Queue"="Overlay+1" }
	    ZTest Always
	    Blend SrcAlpha OneMinusSrcAlpha
	    
	    Pass 
	    {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			
			uniform sampler2D _MainTex;
			uniform float4 _Color;
			uniform float _NoEnergyColorMultiplier;
			uniform float _TransitionWidth;
			uniform float _UVMin;
			uniform float _UVMax;
			uniform float _Progress;
			
			struct v2f 
			{
			    float4 pos : POSITION;
			    float2 uv : TEXCOORD0;
			};
			
			v2f vert (appdata_base v)
			{
			    v2f o;
			    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
			    o.uv = TRANSFORM_UV(0);
			
			    return o;
			}
			
			half4 frag( v2f i ) : COLOR
			{
			    half4 color = tex2D(_MainTex, i.uv);
			    float fOutOfGaugePercent = smoothstep(_UVMin, _UVMax, i.uv.x) - _Progress;
			    
			   
			   	// Avoid the conditionnal branching
	    		color = color + 
	    		(fOutOfGaugePercent > 0)
	    		* 
	    		(	-color 
	    			+ 
	    			(
	    				color 
		    			* 
		    			(
			    			_NoEnergyColorMultiplier 
				    		+ (fOutOfGaugePercent < _TransitionWidth) 
				    		* (1.0f - _NoEnergyColorMultiplier) * (1.0f - fOutOfGaugePercent/_TransitionWidth)
		    			)
		    		)
		    	);
		    	
			    return color*_Color;
			}
			
			ENDCG
		}

	}
}